/**
 *
 */
package com.googlecode.bip.dao;

import java.io.Closeable;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.NamingException;
import cx.ath.choisnet.sql.SimpleDataSource;

/**
 * Resolve database Connection
 *
 * @author Claude CHOISNET
 */
@Deprecated
public abstract class BipConnectionDAO
    extends BipDAO
        implements Closeable
{
    private static final long serialVersionUID = 1L;
    private transient Connection connection;

    /**
     * Create a new {@link Connection} if not already initialized
     *
     * @return a new Connection
     * @throws NamingException
     * @throws SQLException
     */
    final
    protected Connection getConnection()
        throws NamingException, SQLException
    {
        if( connection == null ) {
            //SimpleConnection sds = new SimpleConnection();
            //connection           = sds.getConnection();
            SimpleDataSource sds = new SimpleDataSource( getDataSource() );
            connection           = sds.createConnectionFromDataSource();
            }

        return connection;
    }

    /**
     * Close Connection if needed
     */
    public void close()
    {
        if( connection != null ) {
            try {
                connection.close();
                }
            catch( SQLException ignore ) {
                // ignore
                }
        }
    }

    /**
     * Close a statement
     *
     * @param statement to close, could be null
     */
    protected void closeQuiet( final Statement statement )
    {
        if( statement != null ) {
            try {
                statement.close();
                }
            catch( SQLException ignore ) {
                // ignore
                }
        }
    }

    protected void finalize() throws Throwable
    {
        close();
        super.finalize();
    }

    /*
    private class SimpleConnection extends SimpleDataSource
    {
        SimpleConnection() throws NamingException
        {
            super( BipConnectionDAO.this.getDataSource() );
        }

        Connection getConnection() throws SQLException
        {
            return super.getConnectionFromDataSource();
        }
    }*/
}

/*

Usage

public boolean insertXxxx( final IXxx xxx )
    throws NamingException, SQLException
{
    final String sql1 = "INSERT INTO `xxx` (`...`, `...`) VALUES ("
        + xxx.get...()
        + ", "
        + xxx.get...()
        + ");";
    final String sql2 = "";

    slogger.debug( "sql1 = " + sql1 );

    Connection connection = super.getConnection();
    Statement  statement  = connection.createStatement();

//    if( ! connection.getMetaData().supportsBatchUpdates() ) {
//        throw new Exception
//    }
    try {
        connection.setAutoCommit(false);
        statement.clearBatch(); //on supprimme les anciens batch
        statement.addBatch( sql1 );
        statement.addBatch("UPDATE ...");
        statement.addBatch("...");

        int[] resultat = statement.executeBatch();
        //voir les dints types de retour possibles
        connection.commit();
        connection.setAutoCommit(true);
        }
    finally {
        closeQuiet( statement );
        statement = null;
        }

    return false;
}
*/
